package com.zjgsu.coursecalendar.controller;

import com.zjgsu.coursecalendar.exception.NotExistException;
import com.zjgsu.coursecalendar.model.User;
import com.zjgsu.coursecalendar.service.UserService;
import com.zjgsu.coursecalendar.template.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /*添加用户*/
    @RequestMapping(value = "/adduser")
    public ResultBean addUser(int userId,String userAccount,String userPassword){
        ResultBean resultBean=new ResultBean();
        User user = new User();
        user.setUserId(userId);
        user.setUserAccount(userAccount);
        user.setUserPassword(userPassword);
        try {
            userService.add(user);
        }catch (NotExistException e){
            resultBean.wrongParameter();
            e.printStackTrace();
        }
        return resultBean;
    }

    /*删除用户*/
    @RequestMapping(value = "/deleteuser")
    public ResultBean deleteUser(Integer userid){
        ResultBean resultBean=new ResultBean();
        try {
            userService.delete(userid);
        }catch (NotExistException e){
            resultBean.wrongParameter();
            e.printStackTrace();
        }
        return resultBean;
    }

    /*修改用户*/
    @RequestMapping(value = "/updatauser")
    public ResultBean updataUser(User user,Integer userid){
        ResultBean resultBean=new ResultBean();
        try {
            userService.updata(user,userid);
        }catch (NotExistException e){
            resultBean.wrongParameter();
            e.printStackTrace();
        }
        return resultBean;
    }

    /*查询用户*/
    @RequestMapping(value = "/finduser")
    public ResultBean findUser(@Param("userid") Integer userid){
        ResultBean resultBean=new ResultBean();
        try {
            resultBean.setData(userService.find(userid));
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultBean;
    }
}
